<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Set callback for connection close</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="eventhttpconnection.makerequest.html">EventHttpConnection::makeRequest</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="eventhttpconnection.setlocaladdress.html">EventHttpConnection::setLocalAddress</a></div>
 <div class="up"><a href="class.eventhttpconnection.html">EventHttpConnection</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="eventhttpconnection.setclosecallback" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">EventHttpConnection::setCloseCallback</h1>
  <p class="verinfo">(PECL event &gt;= 1.8.0)</p><p class="refpurpose"><span class="refname">EventHttpConnection::setCloseCallback</span> &mdash; <span class="dc-title">Set callback for connection close</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-eventhttpconnection.setclosecallback-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span>
   <span class="type"><span class="type void">void</span></span> 
   <span class="methodname"><strong>EventHttpConnection::setCloseCallback</strong></span>
    ( <span class="methodparam">
    <span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span>
     <code class="parameter">$callback</code>
   </span>
   [, <span class="methodparam">
    <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span>
     <code class="parameter">$data</code>
   </span>
  ] )</div>

  <p class="para rdfs-comment">
   Sets callback for connection close.
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-eventhttpconnection.setclosecallback-parameters">
  <h3 class="title">参数</h3>
  <dl>

   
    <dt>

     <em><code class="parameter">callback</code></em>
    </dt>

    <dd>

     <p class="para">
      Callback which is called when connection is closed. Should match the
      following prototype:
     </p>
     <div class="methodsynopsis dc-description">
      <span class="type"><span class="type void">void</span></span> 
      <span class="methodname"><strong>callback</strong></span>
       ([ <span class="methodparam">
       <span class="type"><a href="class.eventhttpconnection.html" class="type EventHttpConnection">EventHttpConnection</a></span>
        <code class="parameter">$conn</code>
       <span class="initializer"> = <strong><code>NULL</code></strong></span>
      </span>
      [, <span class="methodparam">
       <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span>
        <code class="parameter">$arg</code>
       <span class="initializer"> = <strong><code>NULL</code></strong></span>
      </span>
     ]] )</div>

    </dd>

   
  </dl>

 </div>

 <div class="refsect1 returnvalues" id="refsect1-eventhttpconnection.setclosecallback-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   没有返回值。
  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-eventhttpconnection.setclosecallback-examples">
  <h3 class="title">范例</h3>
  <div class="example" id="example-4177">
   <p><strong>Example #1 
    <span class="methodname"><strong>EventHttpConnection::setCloseCallback()</strong></span> example</strong></p>
   <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">/*<br />&nbsp;*&nbsp;Setting&nbsp;up&nbsp;close-connection&nbsp;callback<br />&nbsp;*<br />&nbsp;*&nbsp;The&nbsp;script&nbsp;handles&nbsp;closed&nbsp;connections&nbsp;using&nbsp;HTTP&nbsp;API.<br />&nbsp;*<br />&nbsp;*&nbsp;Usage:<br />&nbsp;*&nbsp;1)&nbsp;Launch&nbsp;the&nbsp;server:<br />&nbsp;*&nbsp;$&nbsp;php&nbsp;examples/http_closecb.php&nbsp;4242<br />&nbsp;*<br />&nbsp;*&nbsp;2)&nbsp;Launch&nbsp;a&nbsp;client&nbsp;in&nbsp;another&nbsp;terminal.&nbsp;Telnet-like<br />&nbsp;*&nbsp;session&nbsp;should&nbsp;look&nbsp;like&nbsp;the&nbsp;following:<br />&nbsp;*<br />&nbsp;*&nbsp;$&nbsp;nc&nbsp;-t&nbsp;127.0.0.1&nbsp;4242<br />&nbsp;*&nbsp;GET&nbsp;/&nbsp;HTTP/1.0<br />&nbsp;*&nbsp;Connection:&nbsp;close<br />&nbsp;*<br />&nbsp;*&nbsp;The&nbsp;server&nbsp;will&nbsp;output&nbsp;something&nbsp;similar&nbsp;to&nbsp;the&nbsp;following:<br />&nbsp;*<br />&nbsp;*&nbsp;HTTP/1.0&nbsp;200&nbsp;OK<br />&nbsp;*&nbsp;Content-Type:&nbsp;multipart/x-mixed-replace;boundary=boundarydonotcross<br />&nbsp;*&nbsp;Connection:&nbsp;close<br />&nbsp;*<br />&nbsp;*&nbsp;&lt;html&gt;<br />&nbsp;*<br />&nbsp;*&nbsp;3)&nbsp;Terminate&nbsp;the&nbsp;client&nbsp;connection&nbsp;abruptly,<br />&nbsp;*&nbsp;i.e.&nbsp;kill&nbsp;the&nbsp;process,&nbsp;or&nbsp;just&nbsp;press&nbsp;Ctrl-C.<br />&nbsp;*<br />&nbsp;*&nbsp;4)&nbsp;Check&nbsp;if&nbsp;the&nbsp;server&nbsp;called&nbsp;_close_callback.<br />&nbsp;*&nbsp;The&nbsp;script&nbsp;should&nbsp;output&nbsp;"_close_callback"&nbsp;string&nbsp;to&nbsp;standard&nbsp;output.<br />&nbsp;*<br />&nbsp;*&nbsp;5)&nbsp;Check&nbsp;if&nbsp;the&nbsp;server's&nbsp;process&nbsp;has&nbsp;no&nbsp;orphaned&nbsp;connections,<br />&nbsp;*&nbsp;e.g.&nbsp;with&nbsp;`lsof`&nbsp;utility.<br />&nbsp;*/<br /><br /></span><span style="color: #007700">function&nbsp;</span><span style="color: #0000BB">_close_callback</span><span style="color: #007700">(</span><span style="color: #0000BB">$conn</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">__FUNCTION__</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">PHP_EOL</span><span style="color: #007700">;<br />}<br /><br />function&nbsp;</span><span style="color: #0000BB">_http_default</span><span style="color: #007700">(</span><span style="color: #0000BB">$req</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$dummy</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$conn&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$req</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getConnection</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$conn</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setCloseCallback</span><span style="color: #007700">(</span><span style="color: #DD0000">'_close_callback'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">NULL</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*<br />&nbsp;&nbsp;&nbsp;&nbsp;By&nbsp;enabling&nbsp;Event::READ&nbsp;we&nbsp;protect&nbsp;the&nbsp;server&nbsp;against&nbsp;unclosed&nbsp;conections.<br />&nbsp;&nbsp;&nbsp;&nbsp;This&nbsp;is&nbsp;a&nbsp;peculiarity&nbsp;of&nbsp;Libevent.&nbsp;The&nbsp;library&nbsp;disables&nbsp;Event::READ&nbsp;events<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;on&nbsp;this&nbsp;connection,&nbsp;and&nbsp;the&nbsp;server&nbsp;is&nbsp;not&nbsp;notified&nbsp;about&nbsp;terminated<br />&nbsp;&nbsp;&nbsp;&nbsp;connections.<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;So&nbsp;each&nbsp;time&nbsp;client&nbsp;terminates&nbsp;connection&nbsp;abruptly,&nbsp;we&nbsp;get&nbsp;an&nbsp;orphaned<br />&nbsp;&nbsp;&nbsp;&nbsp;connection.&nbsp;For&nbsp;instance,&nbsp;the&nbsp;following&nbsp;is&nbsp;a&nbsp;part&nbsp;of&nbsp;`lsof&nbsp;-p&nbsp;$PID&nbsp;|&nbsp;grep&nbsp;TCP`<br />&nbsp;&nbsp;&nbsp;&nbsp;command&nbsp;after&nbsp;client&nbsp;has&nbsp;terminated&nbsp;connection:<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;57-php&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;15057&nbsp;ruslan&nbsp;&nbsp;6u&nbsp;&nbsp;unix&nbsp;0xffff8802fb59c780&nbsp;&nbsp;&nbsp;0t0&nbsp;&nbsp;125187&nbsp;socket<br />&nbsp;&nbsp;&nbsp;&nbsp;58:php&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;15057&nbsp;ruslan&nbsp;&nbsp;7u&nbsp;&nbsp;IPv4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;125189&nbsp;&nbsp;&nbsp;0t0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TCP&nbsp;*:4242&nbsp;(LISTEN)<br />&nbsp;&nbsp;&nbsp;&nbsp;59:php&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;15057&nbsp;ruslan&nbsp;&nbsp;8u&nbsp;&nbsp;IPv4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;124342&nbsp;&nbsp;&nbsp;0t0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;TCP&nbsp;localhost:4242-&gt;localhost:37375&nbsp;(CLOSE_WAIT)<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;where&nbsp;$PID&nbsp;is&nbsp;our&nbsp;process&nbsp;ID.&nbsp;<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;The&nbsp;following&nbsp;block&nbsp;of&nbsp;code&nbsp;fixes&nbsp;such&nbsp;kind&nbsp;of&nbsp;orphaned&nbsp;connections.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$bev&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$req</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">getBufferEvent</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$bev</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">enable</span><span style="color: #007700">(</span><span style="color: #0000BB">Event</span><span style="color: #007700">::</span><span style="color: #0000BB">READ</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$req</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">addHeader</span><span style="color: #007700">(</span><span style="color: #DD0000">'Content-Type'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'multipart/x-mixed-replace;boundary=boundarydonotcross'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">EventHttpRequest</span><span style="color: #007700">::</span><span style="color: #0000BB">OUTPUT_HEADER</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$buf&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EventBuffer</span><span style="color: #007700">();<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$buf</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">add</span><span style="color: #007700">(</span><span style="color: #DD0000">'&lt;html&gt;'</span><span style="color: #007700">);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$req</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">sendReply</span><span style="color: #007700">(</span><span style="color: #0000BB">200</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">"OK"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$req</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">sendReplyChunk</span><span style="color: #007700">(</span><span style="color: #0000BB">$buf</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$port&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">4242</span><span style="color: #007700">;<br />if&nbsp;(</span><span style="color: #0000BB">$argc&nbsp;</span><span style="color: #007700">&gt;&nbsp;</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$port&nbsp;</span><span style="color: #007700">=&nbsp;(int)&nbsp;</span><span style="color: #0000BB">$argv</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">];<br />}<br />if&nbsp;(</span><span style="color: #0000BB">$port&nbsp;</span><span style="color: #007700">&lt;=&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">||&nbsp;</span><span style="color: #0000BB">$port&nbsp;</span><span style="color: #007700">&gt;&nbsp;</span><span style="color: #0000BB">65535</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;exit(</span><span style="color: #DD0000">"Invalid&nbsp;port"</span><span style="color: #007700">);<br />}<br /><br /></span><span style="color: #0000BB">$base&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EventBase</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$http&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">EventHttp</span><span style="color: #007700">(</span><span style="color: #0000BB">$base</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$http</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">setDefaultCallback</span><span style="color: #007700">(</span><span style="color: #DD0000">"_http_default"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">NULL</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$http</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bind</span><span style="color: #007700">(</span><span style="color: #DD0000">"0.0.0.0"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$port</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$base</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">loop</span><span style="color: #007700">();</span>
</span>
</code></div>
   </div>

  </div>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="eventhttpconnection.makerequest.html">EventHttpConnection::makeRequest</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="eventhttpconnection.setlocaladdress.html">EventHttpConnection::setLocalAddress</a></div>
 <div class="up"><a href="class.eventhttpconnection.html">EventHttpConnection</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
